public class Islands {
	public int beachLength(String[] kingdom) {
		int answer = 0;
		if(kingdom[0].length()==1){
			for(int i=0;i<kingdom.length-1;i++){
				if(!kingdom[i].equals(kingdom[i+1]))answer++;
			}
			return answer;
		}
		for (int i = 0; i < kingdom.length - 1; i++) {
			if (i % 2 == 0) {
				int length = kingdom[i].length();
				if (!kingdom[i].substring(0, 1).equals(
						kingdom[i].substring(1, 2)))
					answer++;
				if (!kingdom[i].substring(0, 1).equals(
						kingdom[i + 1].substring(0, 1)))
					answer++;
				for (int j = 1; j < length - 1; j++) {
					if (!kingdom[i].substring(j, j + 1).equals(
							kingdom[i].substring(j + 1, j + 2)))
						answer++;
					if (!kingdom[i].substring(j, j + 1).equals(
							kingdom[i + 1].substring(j, j + 1)))
						answer++;
					if (!kingdom[i].substring(j, j + 1).equals(
							kingdom[i + 1].substring(j - 1, j)))
						answer++;
				}
				if (!kingdom[i].substring(length - 1, length).equals(
						kingdom[i + 1].substring(length - 1, length)))
					answer++;
				if (!kingdom[i].substring(length - 1, length).equals(
						kingdom[i + 1].substring(length - 2, length - 1)))
					answer++;
			} else {
				int length = kingdom[i].length();
				for (int j = 0; j < length - 1; j++) {
					if (!kingdom[i].substring(j, j + 1).equals(
							kingdom[i].substring(j + 1, j + 2)))
						answer++;
					if (!kingdom[i].substring(j, j + 1).equals(
							kingdom[i + 1].substring(j, j + 1)))
						answer++;
					if (!kingdom[i].substring(j, j + 1).equals(
							kingdom[i + 1].substring(j + 1, j + 2)))
						answer++;
				}
				if (!kingdom[i].substring(length - 1, length).equals(
						kingdom[i + 1].substring(length - 1, length)))
					answer++;
			}
		}
		for (int i = 0; i < kingdom[kingdom.length - 1].length() - 1; i++) {
			if (!kingdom[kingdom.length - 1].substring(i, i + 1).equals(
					kingdom[kingdom.length - 1].substring(i + 1, i + 2)))
				answer++;
		}
		return answer;
	}

	// BEGIN CUT HERE
	public static void main(String[] args) {
		if (args.length == 0) {
			IslandsHarness.run_test(-1);
		} else {
			for (int i = 0; i < args.length; ++i)
				IslandsHarness.run_test(Integer.valueOf(args[i]));
		}
	}
	// END CUT HERE
}

// BEGIN CUT HERE
class IslandsHarness {
	public static void run_test(int casenum) {
		if (casenum != -1) {
			if (runTestCase(casenum) == -1)
				System.err.println("Illegal input! Test case " + casenum
						+ " does not exist.");
			return;
		}

		int correct = 0, total = 0;
		for (int i = 0;; ++i) {
			int x = runTestCase(i);
			if (x == -1) {
				if (i >= 100)
					break;
				continue;
			}
			correct += x;
			++total;
		}

		if (total == 0) {
			System.err.println("No test cases run.");
		} else if (correct < total) {
			System.err.println("Some cases FAILED (passed " + correct + " of "
					+ total + ").");
		} else {
			System.err.println("All " + total + " tests passed!");
		}
	}

	static boolean compareOutput(int expected, int result) {
		return expected == result;
	}

	static String formatResult(int res) {
		return String.format("%d", res);
	}

	static int verifyCase(int casenum, int expected, int received) {
		System.err.print("Example " + casenum + "... ");
		if (compareOutput(expected, received)) {
			System.err.println("PASSED");
			return 1;
		} else {
			System.err.println("FAILED");
			System.err.println("    Expected: " + formatResult(expected));
			System.err.println("    Received: " + formatResult(received));
			return 0;
		}
	}

	static int runTestCase(int casenum) {
		switch (casenum) {
		case 0: {
			String[] kingdom = { ".#...#.." };
			int expected__ = 4;

			return verifyCase(casenum, expected__, new Islands()
					.beachLength(kingdom));
		}
		case 1: {
			String[] kingdom = { "..#.##", ".##.#.", "#.#..." };
			int expected__ = 19;

			return verifyCase(casenum, expected__, new Islands()
					.beachLength(kingdom));
		}
		case 2: {
			String[] kingdom = { "#...#.....", "##..#...#." };
			int expected__ = 15;

			return verifyCase(casenum, expected__, new Islands()
					.beachLength(kingdom));
		}
		case 3: {
			String[] kingdom = { "....#.", ".#....", "..#..#", "####.." };
			int expected__ = 24;

			return verifyCase(casenum, expected__, new Islands()
					.beachLength(kingdom));
		}

			// custom cases

			
			 case 4: { String[] kingdom = {".", "#", "#", "#", ".", "#", "#",
			 "#", ".", "#", ".", ".", "#", "#", "#", "#", "#", ".", "#", ".",
			 "#", ".", "#", "#", "#", ".", ".", "#", ".", "#", "#", ".", ".",
			 ".", ".", ".", "#", ".", ".", "#", ".", ".", ".", ".", "#", ".",
			 "#", ".", ".", "#"}; int expected__ =27 ;
			 return verifyCase(casenum, expected__, new
			  Islands().beachLength(kingdom)); }
			/*
			 * case 5: { String[] kingdom = ; int expected__ = ;
			 * 
			 * return verifyCase(casenum, expected__, new
			 * Islands().beachLength(kingdom)); }
			 */
			/*
			 * case 6: { String[] kingdom = ; int expected__ = ;
			 * 
			 * return verifyCase(casenum, expected__, new
			 * Islands().beachLength(kingdom)); }
			 */
		default:
			return -1;
		}
	}
}

// END CUT HERE
